/**
 * Copying (c) Qixin Technoligies Co.,Ltd.2019-2029.all rights reserved.
 */

package com.example.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisAnnos;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.websocket.server.PathParam;
import java.util.Collections;

/**
 * 创  建   时  间： 2020/8/1
 * 版           本: V1.0
 * 作           者: qixin
 * 版  权   所  有: 版权所有(C)2019-2029
 * 公           司: 深圳市七智星科技有限公司
 */
@RestController
public class ParamRuleController {
    private String resourceName="com.example.controller.ParamRuleController:sayHello(java.lang.String)";

    @PostConstruct
    public void initParamRule(){
        ParamFlowRule rule=new ParamFlowRule(resourceName);
        rule.setParamIdx(0);
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(1);
        ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
    }
    /*  @GetMapping("/hello")
      public String sayHello(@PathParam("id")String id,@PathParam("name")String name){
          Entry entry=null;
          try {
              entry=SphU.entry(resourceName, EntryType.IN,1,id);
              return "access success";
          } catch (BlockException e) {
              e.printStackTrace();
              return "block";
          }finally {
              if(entry!=null) {
                  entry.exit();
              }
          }
      }*/
    @SentinelResource
    @GetMapping("/hello")
    public String sayHello(@PathParam("id")String id){
        return "access success";
    }
}
